package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.dto.AdminSaveDivisionDto;
import com.example.medical.entity.dto.AdminUpdateDivisionDto;
import com.example.medical.entity.pojo.Division;
import com.example.medical.entity.pojo.Sector;
import com.example.medical.service.DivisionService;
import com.example.medical.mapper.DivisionMapper;
import com.example.medical.service.SectorService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author 24354
* @description 针对表【division】的数据库操作Service实现
* @createDate 2024-03-31 22:42:14
*/
@Service
public class DivisionServiceImpl extends ServiceImpl<DivisionMapper, Division>
    implements DivisionService{

    @Autowired
    private DivisionMapper divisionMapper;

    @Override
    public Result AdminGetDivisionList(GetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        IPage<Division> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Division> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getMemo())) {
            wrapper.like(Division::getDivisionName,dto.getMemo()).or().like(Division::getDivisionSectorName,dto.getMemo());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Autowired
    private SectorService sectorService;
    @Override
    public Result AdminUpdateDivision(AdminUpdateDivisionDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Division checkDivision = getById(dto.getId());
        if (checkDivision==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            checkDivision.setDivisionName(dto.getName());
        }
        if (dto.getSectorId()!=null) {
            Sector checkSector = sectorService.getById(dto.getSectorId());
            if (checkSector==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            checkDivision.setDivisionSectorId(checkSector.getSectorId());
            checkDivision.setDivisionSectorName(checkSector.getSectorName());
        }
        boolean checkUpdate = updateById(checkDivision);
        if (!checkUpdate) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminSaveDivision(AdminSaveDivisionDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Sector checkSector = sectorService.getById(dto.getSectorId());
        if (checkSector==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Division division=new Division();
        division= division.builder()
                .divisionName(dto.getName())
                .divisionSectorId(checkSector.getSectorId())
                .divisionSectorName(checkSector.getSectorName())
                .saveTime(String.valueOf(LocalDateTime.now())).build();
        boolean checkSave = save(division);
        if (!checkSave) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result AdminRemoveDivisionArr(String[] ids) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        for (String id : ids) {
            boolean checkRemove = removeById(id);
            if (!checkRemove) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }
}




